﻿@model BussinessLogic.Models.Study.StudyProtocol
@{
    ViewBag.Title = "Protocol";

    int groupIndex;
    string groupId;

    int sessionIndex;
    string sessionId;

    int exerciseIndex;
    string exerciseId;
}
@functions{
    public List<SelectListItem> getExercises(int id)
    {
        List<SelectListItem> exercises = new List<SelectListItem>();

        foreach (var exercise in Model.Activities)
        {
            var ex = new SelectListItem { Value = exercise.ID.ToString(), Text = exercise.Name };
            if (exercise.ID == id)
            {
                ex.Selected = true;
            }
            exercises.Add(ex);
        }

        return exercises;
    }
}
<h2>
    @Model.StudyName</h2>
@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.StudyID);

    <div style="display: none">
        <div id="clone_groupId">
            <fieldset>
                @Html.TextBox("gName", "New Group")
                @Html.TextBox("gRepeat", 1, new { style = "width:20px" })
                <img src="../../Content/images/delete.png" alt="" onclick="deleteGroup()" />
                <ul id="gSessions">
                    <li id="clone_sessionId">
                        @Html.TextBox("sName", "New Session")
                        @Html.TextBox("sRepeat", 1, new { style = "width:20px" })
                        <img src="../../Content/images/delete.png" alt="" onclick="deleteSession('clone_sessionId')" />
                        <ul id="sExercises">
                            <li id="clone_exerciseId">
                                @Html.DropDownList("eId", getExercises(-1))
                                @Html.TextBox("eRepeat", 1, new { style = "width:20px" })
                                <img src="../../Content/images/delete.png" alt="" onclick="deleteExercise('clone_sessionId', 'clone_exerciseId')" />
                            </li>
                        </ul>
                        <p>
                            <input id='btnAddExercise' type="button" name="btnAddExercise" value="Add Exercise"
                                onclick="addExercise('clone_groupId','clone_sessionId')" />
                        </p>
                    </li>
                </ul>
                <p>
                    <input id='btnAddSession' type="button" name="btnAddSession" value="Add Session"
                        onclick="addSession('clone_groupId')" />
                </p>
            </fieldset>
        </div>
    </div>
    
    @Html.ValidationSummary(true)
                                   groupIndex = 0;

    <div id="sessionGroups">
        @foreach (var group in Model.Groups)
        {
            groupId = "group" + groupIndex;
            <div id="@groupId">
                <fieldset>
                    @Html.TextBox("gName", group.Name)
                    @Html.TextBox("gRepeat", group.Repeat, new { style = "width:20px" })
                    @if (Model.Groups.Count == 1)
                    {
                        <img src="../../Content/images/delete.png" alt="" onclick="deleteGroup('@groupId')" style="display:none"/>
                    }
                    else
                    {
                        <img src="../../Content/images/delete.png" alt="" onclick="deleteGroup('@groupId')"/>
                    }
                    @{sessionIndex = 0;}
                    <ul id="gSessions">
                        @foreach (var session in group.Sessions)
                        {
                            sessionId = "session" + sessionIndex;
            
                            <li id="@sessionId">
                                @Html.TextBox("sName", session.Name)
                                @Html.TextBox("sRepeat", session.Repeat, new { style = "width:20px" })
                                @if (group.Sessions.Count == 1)
                                {
                                    <img src="../../Content/images/delete.png" alt="" onclick="deleteSession('@groupId','@sessionId')" style="display:none"/>
                                }
                                else
                                {
                                    <img src="../../Content/images/delete.png" alt="" onclick="deleteSession('@groupId','@sessionId')"/>
                                }
                                <ul id="sExercises">
                                    @{exerciseIndex = 0;}
                                    @foreach (var exercise in session.Exercises)
                                    {
                                        exerciseId = "exercise" + exerciseIndex;
                                        <li id="@exerciseId">
                                            @Html.DropDownList("eId", getExercises(exercise.ID))
                                            @Html.TextBox("eRepeat", exercise.Repeat, new { style = "width:20px" })
                                            @if (session.Exercises.Count == 1)
                                            {
                                                <img src="../../Content/images/delete.png" alt="" onclick="deleteExercise('@groupId','@sessionId', '@exerciseId')" style="display:none"/>
                                            }
                                            else
                                            {
                                                <img src="../../Content/images/delete.png" alt="" onclick="deleteExercise('@groupId','@sessionId', '@exerciseId')"/>
                                            }
                                        </li>
                                            exerciseIndex++;
                                    }
                                </ul>
                                <p>
                                    <input type="button" name="btnAddExercise" value="Add Exercise" onclick="addExercise('@groupId','@sessionId')" />
                                </p>
                            </li>
                                    sessionIndex++;
                        }
                    </ul>
                    <p>
                        <input type="button" name="btnAddSession" value="Add Session" onclick="addSession('@groupId')" />
                    </p>
                </fieldset>
            </div>
                        groupIndex++;
        }
    </div>
    <input type="button" name="btnAddGroup" value="Add Group" onclick="addGroup()" />
    
    <br />
    <br />
    <input id="jsonResult" type="text" name="jsonResult" style="display: none" />
        if (Model.IsRunning)
        {
        
    <script type="text/javascript">
        $(document).ready(function () {
            hideButtons();
        })
    </script>
        }
        else
        {
    <input type="submit" name="Save" value="Save" />    
        }
    
    <script src="../../Scripts/Protocol/Protocol.js" type="text/javascript"></script>
}
<script type="text/javascript">
    function hideButtons() {
        $('[name="btnAddGroup"]').each(function () {
            $(this).hide();
        });
        $('[name="btnAddSession"]').each(function () {
            $(this).hide();
        });
        $('[name="btnAddExercise"]').each(function () {
            $(this).hide();
        });

        $('[src="../../Content/images/delete.png"]').each(function () {
            $(this).hide();
        });
    }
</script>
